﻿using System;
using System.Web.UI;
using RoketSolutions.SP2010.ImageRotatorWebPart.WebParts.ImageRotator;
using RoketSolutions.SP2010.Infrostructure.Common;
using RoketSolutions.SP2010.Infrostructure.UI.Controls;
using RoketSolutions.SP2010.Infrostructure.UI.Controls.Interfaces;

namespace RoketSolutions.SP2010.ImageRotatorWebPart.ControlTemplates
{
    public partial class ImageRotatorToolPartControl : UserControlBase, IToolPartControl<ImageRotatorSettings>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsInitPhase)
            {
                BindData();
            }
        }

        #region / Properties /

        public Control Control
        {
            get { return this; }
        }

        ImageRotatorSettings _currentSettings;
        private ImageRotatorSettings CurrentSettings
        {
            get { return _currentSettings ?? (_currentSettings = ViewState["CurrentSettings"] as ImageRotatorSettings); }
            set { ViewState["CurrentSettings"] = _currentSettings = value; }
        }

        private bool IsInitPhase { get; set; }

        #endregion

        #region / Methods /

        public void Initialize(ImageRotatorSettings settings)
        {
            if (CurrentSettings == null)
            {
                CurrentSettings = settings;
                IsInitPhase = true;
            }
        }

        public ImageRotatorSettings SaveData()
        {
            CurrentSettings.Effect = DropDownEffects.SelectedValue;
            CurrentSettings.PauseOnHover = CheckBoxPauseOnHover.Checked;
            CurrentSettings.ShowPrevNextButton = CheckBoxShowPrevNextButtons.Checked;

            CurrentSettings.Speed = EnumView<SpeedType>.Parse(DropDownEffectSpeed.SelectedValue, SpeedType.Normal).ToString();
            CurrentSettings.PagerType = EnumView<PagerType>.Parse(DropDownPagerType.SelectedValue, PagerType.Bubbles).ToString();

            int timeout;
            CurrentSettings.Timeout = int.TryParse(TextBoxTimeout.Text, out timeout) ? timeout : 3000;

            return CurrentSettings;
        }

        private void BindData()
        {
            TextBoxTimeout.Text = CurrentSettings.Timeout.ToString();
            CheckBoxPauseOnHover.Checked = CurrentSettings.PauseOnHover;
            CheckBoxShowPrevNextButtons.Checked = CurrentSettings.ShowPrevNextButton;

            BindDropDown(DropDownEffects, null, null, null, CurrentSettings.Effect);
            BindDropDown(DropDownEffectSpeed, null, null, null, CurrentSettings.Speed);
            BindDropDown(DropDownPagerType, null, null, null, CurrentSettings.PagerType);
        }

        #endregion

    }
}
